package com.frain.springcloud.common;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.frain.springcloud.common.entity.TokenEntity;
import com.frain.springcloud.util.HttpRequest;

/**
 * 基础支持中的access_token
 * @ClassName: AccessTokenUtil 
 * @Description: 获取token工具类
 * @author frain
 * @date 2018年9月1日 下午7:56:59 
 *
 */
public class AccessTokenUtil {

    protected static final Logger logger = LoggerFactory.getLogger(AccessTokenUtil.class);

    private static final String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
            + APPConstant.APP_ID + "&secret=" + APPConstant.APP_SECERT;

    private static volatile TokenData token;

    /**
     * 
     * @Title: getToken 
     * @Description: 返回token
     * @param @return 参数说明 
     * @return String 返回类型 
     * @throws 
     * @author frain
     * @date 2018年9月1日 下午7:57:37
     */
    public static String getToken() {

        if (token == null) {

            synchronized (AccessTokenUtil.class) {

                if (token == null) {
                    setToken();

                }
            }

        }

        if (System.currentTimeMillis() > token.getExpireTime()) {
            setToken();
        }

        try {
            return token.getToken();
        }
        catch (Exception e) {
            logger.info("获取token失败");
        }

        return "";

    }

    private static void setToken() {

        for (int i = 0; i < 5; i++) {

            try {
                String string = HttpRequest.sendGet(url, "", null);
                TokenEntity entity = JSONObject.parseObject(string, TokenEntity.class);
                String token = entity.getAccess_token();
                Long exprie = System.currentTimeMillis() + Integer.parseInt(entity.getExpires_in()) * 1000;
                AccessTokenUtil.token = new TokenData(exprie, token);
                break;
            }
            catch (Exception e) {
                logger.info("获取token出错");
            }

            try {
                Thread.sleep(new Random().nextInt(5) * 1000);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

    static class TokenData {

        private Long expireTime;

        private String token;

        public Long getExpireTime() {
            return expireTime;
        }

        public void setExpireTime(Long expireTime) {
            this.expireTime = expireTime;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public TokenData(Long expireTime, String token) {
            super();
            this.expireTime = expireTime;
            this.token = token;
        }

    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {

            System.err.println(getToken());
        }
    }

}
